package com.example.laboratory.ui.core.model.impl;

import com.example.laboratory.bean.*;
import com.example.laboratory.manager.UserInfoManager;
import com.example.laboratory.net.RxSchedulers;
import com.example.laboratory.net.callback.RxObserver;
import com.example.laboratory.net.callback.RxPageListObserver;
import com.example.laboratory.ui.core.model.IUserModel;
import com.google.gson.Gson;
import okhttp3.RequestBody;

import java.util.HashMap;
import java.util.List;

/**
 * author: 康栋普
 * date: 2018/3/21
 */

public class UserModel extends CommonModel implements IUserModel {

    @Override
    public void getRecordList(String uid, RxObserver<Record> rxObserver) {
        doRxRequest()
                .getRecordList("Bearer "+UserInfoManager.getUserInfo().getToken(),uid)
                .compose(RxSchedulers.<Record>io_main())
                .subscribe(rxObserver);
    }

    @Override
    public void getLaboratoryList(int page, int size, String uid,String departId, RxObserver<Laboratory> rxObserver) {
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("labUid",uid);
        paramsMap.put("departId",departId);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        doRxRequest()
                .getLaboratoryList("Bearer "+UserInfoManager.getUserInfo().getToken(),page,size,body)
                .compose(RxSchedulers.<Laboratory>io_main())
                .subscribe(rxObserver);
    }

    @Override
    public void getAllLabs(RxObserver<Laboratory> rxObserver) {
        doRxRequest()
                .getAllLabs("Bearer "+UserInfoManager.getUserInfo().getToken())
                .compose(RxSchedulers.<Laboratory>io_main())
                .subscribe(rxObserver);
    }

    @Override
    public void getUsersByDepartId(String departId, String permission, RxObserver<UserList> rxObserver) {
        doRxRequest()
                .getUsersByDepartId("Bearer "+ UserInfoManager.getUserInfo().getToken(),departId,permission)
                .compose(RxSchedulers.<UserList>io_main())
                .subscribe(rxObserver);
    }

    @Override
    public void addUser(UserList.UserListBean userListBean, RxObserver<String> callback) {
        Gson gson=new Gson();
        String strEntity = gson.toJson(userListBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        doRxRequest()
                .AddUser("Bearer "+ UserInfoManager.getUserInfo().getToken(),body)
                .compose(RxSchedulers.<String>io_main())
                .subscribe(callback);
    }


}
